package com.itheima.spring.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author ：sean
 * @date ：Created in 2020/7/4
 * @description ：业务动态代理类
 * @version: 1.0
 */
public class ServiceDynamicProxy implements InvocationHandler {
	private Object target;

	public ServiceDynamicProxy(Object target) {
		this.target = target;
	}

	/**
	 * 获取被代理接口实例对象
	 * @param <T>
	 * @return
	 */
	public <T> T getProxy() {
		return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("method before");
		Long start = System.currentTimeMillis();
		Object result = method.invoke(target, args);
		Long end = System.currentTimeMillis();
		System.out.println("method after 耗时(毫秒):"+(end-start));
		return result;
	}
}
